package com.bw.educate.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bw.educate.common.ResponseResult;
import com.bw.educate.common.auth.UserInfo;
import com.bw.educate.entity.EduWebsiteTemplate;
import com.bw.educate.req.WebsiteCommon;
import com.bw.educate.service.IEduWebsiteTemplateService;
import com.bw.educate.util.TestImport;
import com.bw.educate.util.UserUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 在线教育-五组
 * @since 2020-08-12
 */
@RestController
@RequestMapping("eduTemplate")
public class EduWebsiteTemplateController {

    @Autowired
    private IEduWebsiteTemplateService eduWebsiteTemplateService;
    @Autowired
    private TestImport testImport;

    @Autowired
    private UserUtil userUtil;


    /**
     * @Description:模板列表
     * @Author: 周明丽
     * @Date: 2020/8/15 21:55
     * @param websiteAd:
     * @return: com.bw.educate.common.ResponseResult
     **/
    @PostMapping("list/{currentPage}/{pageSize}")
    public ResponseResult list(@PathVariable("currentPage") int currentPage, @PathVariable("pageSize") int pageSize,@RequestBody WebsiteCommon websiteAd){
        ResponseResult result=ResponseResult.SUCCESS();
        try {
            PageHelper.startPage(currentPage,pageSize);
            QueryWrapper queryWrapper=new QueryWrapper();
            if(websiteAd.getName()!=null){
                queryWrapper.like("NAME","%"+websiteAd.getName()+"%");
            }
            List<EduWebsiteTemplate> list=eduWebsiteTemplateService.list(queryWrapper);
            PageInfo pageInfo = new PageInfo(list);
            result.setResult(pageInfo);

        }catch (Exception e){

            result.setSuccess(false);
            result.setMessage("模板列表查询失败"+e.toString());
        }

        return result;

    }

    /**
     * @Description:获取模板列表
     * @Author: 周明丽
     * @Date: 2020/8/16 14:11

     * @return: com.bw.educate.common.ResponseResult
     **/
    @GetMapping("getList")
    public ResponseResult getList(){
        ResponseResult result=ResponseResult.SUCCESS();
        try {

            QueryWrapper queryWrapper=new QueryWrapper();
            queryWrapper.eq("STATUS",1);
            List<EduWebsiteTemplate> list=eduWebsiteTemplateService.list();
            result.setResult(list);

        }catch (Exception e){

            result.setSuccess(false);
            result.setMessage("模板列表查询失败"+e.toString());
        }

        return result;

    }


    /**
     * @Description:删除模板
     * @Author: 周明丽
     * @Date: 2020/8/15 21:51
     * @param id:
     * @return: com.bw.educate.common.ResponseResult
     **/
    @GetMapping("deleteTemplate/{id}")
    public ResponseResult deleteTemplate(@PathVariable("id") Long id){

        ResponseResult result=ResponseResult.SUCCESS();

        try {

            eduWebsiteTemplateService.removeById(id);
            result.setMessage("删除成功");

        }catch (Exception e){
            result.setSuccess(false);
            result.setMessage("模板删除失败"+e.toString());
        }

        return result;
    }


    /**
     * @Description:根据id查询模板
     * @Author: 周明丽
     * @Date: 2020/8/15 21:53
     * @param :
     * @return: com.bw.educate.common.ResponseResult
     **/
//    @GetMapping("findPoster/{id}")
//    public ResponseResult findPoster(@PathVariable("id") Long id){
//
//        ResponseResult result=ResponseResult.SUCCESS();
//
//        try {
//            result.setResult(eduWebsiteTemplateService.getById(id));
//            result.setMessage("查询成功");
//
//        }catch (Exception e){
//            result.setSuccess(false);
//            result.setMessage("模板查询失败"+e.toString());
//        }
//
//        return result;
//    }

    /**
     * @Description:上传模板
     * @Author: 周明丽
     * @Date: 2020/8/18 10:23
     * @param file:
     * @return: com.bw.educate.common.ResponseResult
     **/
    @PostMapping("importExcel")
    public ResponseResult importExcel(MultipartFile file) {
        ResponseResult result=ResponseResult.SUCCESS();
        try {
            Map map = testImport.uploadtl(file);
            result.setResult(map);
            result.setMessage("上传成功");
        }catch (Exception e){
            result.setSuccess(false);
            result.setMessage("模板上传失败"+e.toString());
        }
        return result;
    }

    /**
     * @Description:修改状态
     * @Author: 周明丽
     * @Date: 2020/8/18 8:02
     * @param id:
     * @return: com.bw.educate.common.ResponseResult
     **/
    @GetMapping("updateTemplate/{id}")
    public ResponseResult updateTemplate(@PathVariable("id") Long id){

        ResponseResult result=ResponseResult.SUCCESS();

        try {

            EduWebsiteTemplate eduWebsiteTemplate = eduWebsiteTemplateService.getById(id);
            if( eduWebsiteTemplate.getStatus()==0){
                eduWebsiteTemplate.setStatus(1);
            }else{
                eduWebsiteTemplate.setStatus(0);
            }
            eduWebsiteTemplateService.updateById(eduWebsiteTemplate);
            result.setMessage("修改成功");

        }catch (Exception e){
            result.setSuccess(false);
            result.setMessage("模板状态修改失败"+e.toString());
        }

        return result;
    }


    /**
     * @Description:保存模板
     * @Author: 周明丽
     * @Date: 2020/8/18 21:00
     * @param eduWebsiteTemplate:
     * @param request:
     * @return: com.bw.educate.common.ResponseResult
     **/
    @PostMapping("save")
    public ResponseResult save(@RequestBody EduWebsiteTemplate eduWebsiteTemplate,HttpServletRequest request){

        ResponseResult result=ResponseResult.SUCCESS();

        try {
            //获取当前登录人
            UserInfo loginUser = userUtil.getLoginUser(request);
            eduWebsiteTemplate.setCreateTime(new Date());
            eduWebsiteTemplate.setUpdateTime(new Date());
            eduWebsiteTemplate.setCreateUser(1L);
            eduWebsiteTemplate.setUpdateUser(1L);
            eduWebsiteTemplate.setStatus(0);
            eduWebsiteTemplateService.saveOrUpdate(eduWebsiteTemplate);
            result.setMessage("保存成功");

        }catch (Exception e){
            result.setSuccess(false);
            result.setMessage("保存模板失败"+e.toString());
        }

        return result;
    }

    /**
     * @Description:根据id查询模板
     * @Author: 周明丽
     * @Date: 2020/8/18 8:02
     * @param id:
     * @return: com.bw.educate.common.ResponseResult
     **/
    @GetMapping("upTemplate/{id}")
    public ResponseResult upTemplate(@PathVariable("id") Long id){

        ResponseResult result=ResponseResult.SUCCESS();

        try {

            EduWebsiteTemplate eduWebsiteTemplate = eduWebsiteTemplateService.getById(id);
            result.setResult(eduWebsiteTemplate);
            result.setMessage("查询成功");

        }catch (Exception e){
            result.setSuccess(false);
            result.setMessage("模板查询失败"+e.toString());
        }

        return result;
    }


}
